6. Searching and Replacing Text



This chapter explains how to use the CodeWarrior IDE facilities to search and replace text in files.

The CodeWarrior IDE provides comprehensive search and replace features with the Find dialog box. You can search for and replace text in a single file, in every file in a project, or in any combination of files. You can also search for regular expressions, like as those used in UNIX's grep command.

The topics in this chapter are:


Guided Tour of the Find Dialog Box

The Find window, shown in Figure 6.1 on page 167 (Windows) and Figure 6.4 on page 173 (Mac OS and Solaris), is a versatile feature of the CodeWarrior IDE. To show the Find dialog box, choose the Find command in the Search Menu. Use this dialog box to perform find and replace operations for text in a single file or for text in multiple files in your project. You can use text strings, text substrings, and pattern matching to carry out find and replace operations.

There are two different sections in the Find dialog box.


Find and Replace Section

This section presents a short tour of the find and replace user interface items in the Find window shown in Figure 6.1 on page 167. The items in the window are:


Find text box

The Find text box is one of the editable text fields in the Find dialog box, shown in Figure 6.1. You enter text in this field that you want to search for.

You can use the Cut, Copy, Paste, and Clear commands with the Find text box. These commands are documented in the section called "Edit Menu" on page 603.

Also, the discussion "Enter `Find' String" on page 608 tells how to enter text into the Find Text Box without using the Find dialog box.

Figure 6.1 The Find Dialog search and replace section




Replace text box

The Replace text box is one of the editable text fields in the Find dialog box, shown in Figure 6.1. The text you enter in this field will be used to replace the text you're searching for.

You can use the Cut, Copy, Paste, and Clear commands with the Find Text Box. These commands are documented in the section called "Edit Menu" on page 603.

Also, the discussion "Enter `Replace' String" on page 608 tells how to enter text into the Replace Text Box without using the Find dialog box.


Recent Strings pop-up menu

The Recent Strings pop-up menu is shown in Figure 6.2. It contains strings that were recently used for searches.

There are actually two of these pop-ups. Each pop-up is to the right of both the Find text box and the Replace text box. Selecting an item in one of these pop-ups enters it in the corresponding text box.

Figure 6.2 Recent Strings pop-up menu





Find button

The Find button, shown in Figure 6.1 on page 167, confirms the contents of the Find dialog box and begins the search. The settings in the dialog box are remembered and are displayed when the Find command is invoked again. The Find button is not available until you complete certain required text fields.

To learn more about finding text, see "Searching for Selected Text" on page 176.


Replace button

The Replace button is one of the buttons in the Find dialog box, shown in Figure 6.1 on page 167.

When you enter text in the Find text box and click the Find button, the CodeWarrior IDE will search for matching text according to the control settings checked at the bottom of the Find dialog box. If a match is found for the text in the Find text box, the Replace button can be clicked to replace the found text with that shown in the Replace text box.

To learn more about searching and replacing text, see "Replacing Found Text" on page 182.


Replace & Find button

The Replace & Find button is shown in Figure 6.1 on page 167. This button behaves much like the Replace button, but also initiates another Find operation after the text substitution is performed.

To learn more about searching and replacing text, see "Finding and Replacing Text in a Single File" on page 178 and "Finding and Replacing Text in Multiple Files" on page 185.


Replace All button

The Replace All button is shown in Figure 6.1 on page 167. This button behaves much like the Replace button, but replaces every occurrence of the text shown in the Find text box with the text shown in the Replace text box.

To learn more about searching and replacing text, see "Replacing Found Text" on page 182.


Batch checkbox

The Batch checkbox is shown in Figure 6.1 on page 167. Selecting this checkbox causes the results of the Find command to appear in a Search Results message window (Figure 6.7 on page 185).

To learn more about the role of the Batch checkbox in searching, see "Using Batch Searches" on page 184.


Wrap checkbox

The Wrap checkbox is shown in Figure 6.1 on page 167. Selecting this checkbox allows a search that reaches one end of a file or group of files to continue from the opposite end.

To learn more about this feature, consult "Controlling Search Range in a Single File" on page 180.


Ignore Case checkbox

The Ignore Case checkbox is shown in Figure 6.1 on page 167. This checkbox causes the CodeWarrior IDE to disregard the case (uppercase or lowercase) of the text entered into the Find text box.

To learn more about this feature, consult "Controlling Search Parameters" on page 181.


Entire Word checkbox

The Entire Word checkbox is shown in Figure 6.1 on page 167. This checkbox causes the CodeWarrior IDE to ignore matching text that occurs within words.

To learn more about this feature, consult "Controlling Search Parameters" on page 181.


Regexp checkbox

The Regexp checkbox is shown in Figure 6.1 on page 167. This checkbox causes the CodeWarrior IDE to interpret the text in the Find text box as a regular expression.

CodeWarrior's regular expressions are similar to the regular expression for grep in UNIX. To learn more about this feature, refer to "Using Regular Expressions (grep)" on page 195.


Multi-File Search Disclosure triangle

The Multi-File Search Disclosure triangle is shown in Figure 6.1 on page 167. Click this triangle to show the Multi-File Search Section of the Find window, as shown in Figure 6.3 on page 171 (Windows) or Figure 6.4 on page 173 (Mac OS and Solaris).

To learn more about multi-file searching with the Find window, see "Finding and Replacing Text in Multiple Files" on page 185.


Multi-File Search button

The Multi-File Search button is shown in Figure 6.3. Click this button to enable or disable the options in the Multi-File Search Section of the Find window, shown in Figure 6.4 on page 173.

When the Multi-File Search button is not depressed, as shown in the dialog box of Figure 6.3, the items in the Multi-File Search Section of the Find window are dimmed.

To learn more about the Multi-File Search Button, see "Activating Multi-File Search" on page 186.

Figure 6.3 Multi-File Search button not selected







Multi-File Search Section

This section describes the user interface items in the Multi-File Search Section of the Find window, shown in Figure 6.4. The user interface items are:


File list

The File list is shown in Figure 6.4. This is a list of the files that will be searched in a Multi-file search. You add files to this list by enabling the Sources, System Headers, Project Headers, and Others controls. You can also drag and drop groups or files from the Project window into the list.

For more information about adding files and removing files in file sets, see "Choosing Files to be Searched" on page 187.


File Sets pop-up menu

The File Sets pop-up menu is shown in Figure 6.5. This pop-up menu is used with Multi-file searches. Use this pop-up menu to select, add, and remove saved sets of files to search and replace.

You can build up sets of files, such as collections of header or interface files, that will be available whenever you want to search through the files for text.

For more information about Multi-file sets, see "Choosing Files to be Searched" on page 187.

Figure 6.4 The Find Dialog Box for a multiple file search



Project pop-up menu

The Project pop-up menu, shown in Figure 6.6, allows you to choose the project file with which you want to perform your search. Since the CodeWarrior IDE can have multiple projects open at a time, this menu provides a way to perform the same search in different projects.


Stop at End of File checkbox

If you turn off the Stop at End of File checkbox, all the files in the File list are searched as one large file. When the CodeWarrior IDE reaches the end of one file, it starts searching the next. When it reaches the end of the last file to search, it beeps.

To search each file individually, enable the Stop at End of File checkbox. When the CodeWarrior IDE reaches the end of a file, it stops searching and beeps. You must choose Find in Next File from the Search Menu to continue the search.

For more information about using the Stop at End of File checkbox, see "Controlling Search Range in a Single File" on page 180.

Figure 6.5 File Sets pop-up menu




Sources checkbox

The Sources checkbox is shown in Figure 6.4 on page 173. This checkbox adds all the source files from the current project to the File list.

For more information about source files in file sets, and their role in Multi-file searches, see "Adding project source files" on page 187.

Figure 6.6 Project Pop-up Menu




System Headers checkbox

The System Headers checkbox is shown in Figure 6.4 on page 173. This checkbox adds all system header or interface files from the current project to the File list.

For more information about system headers in file sets, and their role in multi-file searches, see "Adding system header files" on page 188.


Project Headers checkbox

The Project Headers checkbox is shown in Figure 6.4 on page 173. This checkbox adds all the project header or interface files from the current project to the File list.

For more information about project headers in file sets, and their role in Multi-file searches, see "Adding project header files" on page 188.


Others button

The Others button is shown in Figure 6.4 on page 173. This button and its checkbox allows you to add one or many additional files to the File list.

For more information about adding file to file sets, see "Adding and removing arbitrary files" on page 188.


Searching for Selected Text

The IDE provides two ways of searching for text without using the Find dialog box. In both of these methods, you select text in a window, and the CodeWarrior IDE finds the text for you without displaying the Find dialog box.

When you search for selected text, the CodeWarrior IDE uses the option settings that you last chose in the Find window. To change these option settings, you must use the Find dialog box.

You should know how to select text in the editor window before reading this section. If you don't know how to select text, refer to "Selecting Text" on page 148.


Finding text in the active editor window

This method is useful if you want to find additional occurrences of a text string in the same open editor window that you're working with.

First, select an instance of the text you want to find. After selecting your text, choose Find Selection from the Search Menu.

The CodeWarrior IDE looks for the next occurrence of your text string in the current file only.

To search toward the end of the file for the next occurrence of the text string, click the Find button or choose Find Next from the Search Menu. You can also press the keyboard shortcut shown to the right of the Find Next command in the Search menu.

To search toward the beginning of the file for the previous occurrence of the text string, use the Find Previous command:


Windows

This command is available by pressing the key binding Shift-F3.


Mac OS and Solaris

This command is available when you hold down the Shift key while using the Search Menu.

The CodeWarrior IDE finds the previous occurrence of the text string and selects it. If the string is not found, the CodeWarrior IDE beeps.

Search for more occurrences of the text string by continuing to use Find, Find Next, or Find Previous on the Search Menu.


Finding text in another window

This method is useful when your text string is in one file and you want to search for the same text string in another file.

First, select an instance of the text you want to find. After selecting your text, choose Enter `Find' String from the Search Menu. The editor enters the text in the Find text box of the Find dialog box.

Now make the window you want to search active, Then, choose Find Next from the Search Menu to search forwards in the active editor window for the next occurrence of your text string.

Use the Find Previous command to search backwards in the active editor window for the next occurrence of your text string:


Windows

This command is available by pressing the key binding Shift-F3.


Mac OS and Solaris

This command is available when you hold down the Shift key while using the Search Menu.

The CodeWarrior IDE looks for the Find text box string in the active editor window, starting from the location of the text insertion point in that window.

If you want to search toward the end of the file for the next occurrence of the Find text box string, click the Find button or choose Find Next from the Search Menu. You can also press the keyboard shortcut shown to the right of the Find and Find Next commands in the Search menu.

To search toward the beginning of the file for the previous occurrence of the Find string, use the Find Previous command.

Search for more occurrences of the Find text box string by continuing to use Find, Find Next, or Find Previous from the Search Menu.


Finding and Replacing Text in a Single File

The Find window allows you to search for text patterns in the active editor window. When you find the text you are interested in, you can change it or look for another occurrence of it.

This section discusses how to use the Find dialog box to locate specific text you want to replace in the active editor window.

If you don't yet have a window open, see "Opening an Existing File" on page 108.

If you haven't yet created a file, see "Creating a New File" on page 107.

The topics in this section are:


Finding Search Text

To enter text in the Find text box, bring up the Find window using the Find command in the Search menu. Type a text string into the Find text box on the dialog box, or choose a string from the Recent Strings pop-up menu, as shown in Figure 6.2 on page 168.


NOTE

To learn how to enter search text containing a Return or Tab character, refer to "Searching with Special Characters" on page 181.

Before searching, you can set other search options that control the range of your search.

The search range defines whether you want to search the entire file or just from the text insertion point in one direction. To set up the range of your search, see "Controlling Search Range in a Single File" on page 180.

The search parameters define whether you want to search for text regardless of text case, and whether to search partial words for the text. To set up the parameters of your search, see "Controlling Search Parameters" on page 181.

Before proceeding, make sure that multi-file searching is turned off since you are only interested in searching the active editor window. To learn about how to determine whether multi-file searching is turned off, refer to "Activating Multi-File Search" on page 186.

Click the Find button in the Find window to search forward from the text insertion point in the file, or choose Find or Find Next from the Search Menu. CodeWarrior now searches for the Find text box string in the active editor window.


Windows

Press the key binding Shift-F3 to perform a Find Previous command, which searches backwards from the text insertion point in the file.


Mac OS and Solaris

Hold down the Shift key and choose Find Previous from the Search Menu if you want to search backwards from the text insertion point in the file.

To continue searching toward the end of the file for the next occurrence of the Find text box string, click the Find button or choose Find Next from the Search menu.

To continue searching toward the beginning of the file for the previous occurrence of the Find text box string, use the Find Previous command.

The editor finds and selects the Find text box string. If the string is not found, the editor beeps.

Search for more occurrences of the Find text box string by continuing to use Find, Find Next, or Find Previous.

From this point, you can replace some or all of the text you find with a new text string.

To replace text, see "Replacing Found Text" on page 182.


Controlling Search Range in a Single File

The Wrap checkbox option in the Find dialog box controls the flow of the search when you reach the beginning or end of a file.

For example, suppose that you enable the Wrap checkbox option. When the text insertion point is somewhere in the middle of a source file in the active editor window, and you choose Find Next on the Search Menu, the CodeWarrior IDE searches from the insertion point to the end of the file. When the search reaches the end of the file, the search continues from the beginning of the file to the insertion point. In other words, the search "wraps" around the ends of the file.

The Find Previous command operates in a similar fashion. When the search reaches the beginning of the file and the Wrap checkbox option is enabled, the search continues from the end of the file.

If you have the Wrap checkbox option unchecked, and you choose Find Next on the Search Menu, the search stops when it reaches the end of the file.

If you're searching multiple files with the Wrap checkbox option checked, the CodeWarrior IDE searches from the first file in the file list after it reaches the last file.


Controlling Search Parameters

The Ignore Case checkbox and the Entire Word checkbox are two easily-accessible options in the CodeWarrior IDE that you can use for matching text.


Ignore Case checkbox

The Ignore Case checkbox is shown in Figure 6.1 on page 167. When selected, the CodeWarrior IDE treats uppercase and lowercase text in the Find text box as identical. When deselected, uppercase and lowercase text are distinct.

For example, when you select the Ignore Case checkbox and enter Foobar in the Find text box, the IDE finds occurrences of foobar, FOOBAR, and other possible combinations of uppercase and lowercase characters.


Entire Word checkbox

The Entire Word checkbox is shown in Figure 6.1 on page 167. This checkbox causes the CodeWarrior IDE to find only complete words (delimited by punctuation or white-space characters) matching the text in the Find text box. If deselected, the IDE finds occurrences of the search text embedded within larger words. For example, suppose you enter Word in the Find text box. If the Entire Word checkbox is selected, the IDE finds only the occurrences of Word in the file. If the Entire Word checkbox is deselected, the IDE matches Word even when it is embedded inside other text, such as Words, WordCount, and BigWordCount.


Searching with Special Characters

To enter a Tab or Return character in the Find or Replace fields, use one of the following methods:


Mac OS and Solaris

To directly enter a Tab character, press Option-Tab. To enter a Return character, press Option-Return.


WARNING!

Using Regexp will alter the manner in which CodeWarrior locates a string match. See "Using Regular Expressions (grep)" on page 195 for more information on using Regexp.

Replacing Found Text

When you find an occurrence of text you are interested in, you can either replace one occurrence at a time, or you can replace all occurrences in the entire file.


Selective Replace

To selectively replace text, first enter some text to find, then choose the Find operation on the Search Menu, or click the Find button in the Find dialog box. You can read more about how to find text by referring to "Finding Search Text" on page 178.

Next, enter the replacement text string in the Replace text box field of the Find dialog box.

Type the string in the Replace text box field or choose a string from the Recent Strings pop-up menu of the Replace text box by clicking the arrow icon just to the right. The Recent Strings pop-up menu (Figure 6.2 on page 168) contains the last five strings you have used.

Now choose whether to replace the string you found. For convenience, there are three buttons in the Find dialog box for doing this, the Replace button, the Replace & Find button, and the Replace All button. Each button performs a different operation.

To replace the string and see the results, click the Replace button in the Find dialog box or choose Replace from the Search Menu. The editor replaces the text that was found with the Replace text box string.

To continue searching forward, choose Find Next from the Search Menu, or click the Find button in the Find dialog box.

To continue searching backward, use the Find Previous command, or press the Shift key and click the Find button in the Find dialog box.

To replace the string and find the next occurrence, choose Replace & Find Next from the Search Menu, or click the Replace & Find button in the Find dialog box. The editor replaces the selected text with the Replace text box string and finds the next occurrence of the Find text box string. If the editor can't find another occurrence, it beeps.

To replace the Find text box string and find the previous occurrence, use the Replace & Find Previous command:


Windows

Press the key binding Ctrl-Shift-L, or press the Shift key as you click the Replace & Find button in the Find dialog box.


Mac OS

hold down the Shift key as you choose Replace & Find Previous from the Search Menu, or press the Shift key as you click the Replace & Find button in the Find dialog box.

The editor replaces the selected text with the Find text box string and searches for a previous occurrence of the Find text box string. If the CodeWarrior IDE can't find another occurrence, it beeps.


Replace All

To replace text, first enter some text to find in the Find text box, then choose the Find operation on the Search Menu, or click the Find button in the Find dialog box. You can read more about how to find text by referring to "Finding Search Text" on page 178.

Next, enter the replacement text string in the Replace text box field of the Find dialog box.

To replace all the occurrences of the Find text box string, click the Replace All button in the Find dialog box, or choose Replace All from the Search Menu.


WARNING!

Be careful when you use the Replace All command, since Undo is not available for this operation.

TIP

If you are going to perform a Replace All operation on a single source file, make sure to save the source file before executing the replace operation. In the event that you should change your mind, and before you save any changes, use Revert to replace the modified file in memory with the saved version on disk. This technique will not work across multiple files.

Using Batch Searches

The CodeWarrior IDE gives you a way to collect all matching descriptions of your text search in one window for easy reference.

If the Batch checkbox option is checked in the Find dialog box, and the Find button is clicked, the CodeWarrior IDE searches for all occurrences of the Find text box string and lists them in the Search Results message window, as shown in Figure 6.7.

The Search Results window shown in Figure 6.7 has a List View and a Source View.

To go to a particular occurrence of the Find text box string, so that it is shown in the Source View pane of the window, double-click on its entry in the List View.

To learn more about the features of this window, refer to the discussion of the Message Window in "Guided Tour of the Message Window" on page 373.

Figure 6.7 Batch search results




Finding and Replacing Text in Multiple Files

The CodeWarrior IDE allows you to search multiple files for the occurrence of text strings.

In this section you will learn how to do text searches through multiple files.

Another way to quickly access information and search in multiple files is with the Browser's Go Back and Go Forward commands on the Search menu. To learn about how to use these commands, refer to "Go Back and Go Forward" on page 242.

The topics in this section are:


Activating Multi-File Search

To configure the CodeWarrior IDE to search through multiple files, you need to activate multi-file searching in the Find dialog box.

When the Multi-File Search button is on, the button appears to be depressed.

When the Multi-File Search button is off, the button looks three-dimensional.

Click the Multi-File Search Disclosure triangle to the left of the Multi-File Search button, shown in Figure 6.1 on page 167, so that the triangle points down.

The CodeWarrior IDE displays the Multi-File Search Section, an extension of the Find dialog box, as shown in Figure 6.8 on page 187. When the Multi-File Search button is on (as shown above), the Multi-File Search Section is enabled.

To learn about how to configure the Multi-File Search Section of the Find dialog box, refer to "Choosing Files to be Searched" on page 187, "Saving a File Set" on page 191, "Removing a File Set" on page 192, and "Controlling Search Range in Multiple Files" on page 193.

Figure 6.8 The Find dialog box with multi-file search options




Choosing Files to be Searched

There are several ways to choose files for a search.


Adding project source files

To add all the source files from the current project, turn on the Sources checkbox. When you turn off the Sources checkbox, the CodeWarrior IDE removes all associated files from the file list.

To include only some of the files, turn on the Sources checkbox and delete the files you don't want by selecting them and pressing Backspace/Delete after clicking on the file name.

If turning on this option doesn't add any files, update your project's internal list of header and interface files with the Make command. To learn how to do this, refer to "Making a Project" on page 360.


Adding project header files

To add all the project header or interface files from the current project, turn on the Project Headers checkbox. When you turn off the Project Headers checkbox, the CodeWarrior IDE removes all associated files from the file list.

To include only some of the files, turn on the Project Headers checkbox and delete the files you don't want by selecting them and pressing Backspace/Delete.

If turning on this option doesn't add any files, update your project's internal list of header or interface files with the Make command. To learn how to do this, refer to "Making a Project" on page 360.


Adding system header files

To add all the system header or interface files from the current project, turn on the System Headers checkbox. When you turn off the System Headers checkbox, the CodeWarrior IDE removes all associated files from the file list.

To include only some of the files, turn on the System Headers checkbox and delete the files you don't want by selecting them and pressing Delete.

If turning on this option doesn't add any files, update your project's internal list of header or interface files with the Make command. To learn how to do this, refer to "Making a Project" on page 360.


Adding and removing arbitrary files

For multi-file searches, you can manually add and remove files to your file set. Adding files to a file set enables you to search in files not included in your current project.

First, click the Others button in the Multi-File Search Section of the Find dialog box. The Select Files To Search dialog box will be displayed as shown in Figure 6.9 (Windows), Figure 6.11 (Mac OS), and Figure 6.12 (Solaris).

Figure 6.9 The Select Files To Search dialog box (Windows)


The Select Files To Search dialog box displays a list of the files in the current directory that may be added to the file set. To add a file to the file set, select it and click the Add button. If you change your mind, click Cancel and the file set will not be changed.


Windows

You can select multiple files in this list by pressing the Control key and clicking a file simultaneously. When you're finished choosing files, click Add. All files in the Select Files To Search list will appear in the file set. To remove files from the file set, select them in the Find dialog box (Figure 6.8 on page 187) and press Backspace or Delete.


WARNING!

(Windows) If you select a file's icon in the Select Files To Search dialog box (Figure 6.9) and press the Delete key, the Confirm File Delete dialog box (Figure 6.10) will appear. This dialog box warns you that you are attempting to delete the selected file from your hard drive. Click No to return to the Select Files To Search dialog box. Instead of selecting the file's icon, select the file's name (including the quote marks) in the File Name text box and press Backspace or Delete. This is the correct way to remove a selected file from the Select Files To Search dialog box.

Figure 6.10 Confirm File Delete dialog box (Windows)



Mac OS and Solaris

To add all files in the list, click the Add All button. The files are removed from the top file list and reappear in the Select Files To Search list at the bottom of the dialog box. To remove files from the Select Files To Search list, select them and click the Remove button. To remove all the files from the Select Files To Search list, click the Remove All button. When you're finished choosing files, click Done. All files in the Select Files To Search list will appear in the file set.

To add more files later, just click the Others button in the Find dialog box and repeat the process.


Choosing a file set

To select a previously-saved file set to include in your search, click on the File Sets pop-up menu and choose a file set from the menu, as shown in Figure 6.5 on page 174. The files then appear in the File Sets list.

Figure 6.11 The Select Files To Search dialog box (Mac OS)



Saving a File Set

To save a file set for use in future multi-file searches, choose Save this File Set from the File Sets pop-up menu. The IDE displays the Save File Set dialog box shown in Figure 6.13 on page 193.

Name the file set by entering a name in the Save File Set As text field.

You can choose the scope of projects which can use this file set. There are two scopes to choose from: Specific and Global. Click the radio button next to the scope you want to use with this file set.

If you plan to use this file set only with the current project, click Specific to this project. The CodeWarrior IDE stores the file set in the project.

If you think you'll use this file set with other projects, click Global, for all projects. The CodeWarrior IDE stores the file set in its preferences file, so all projects (even existing projects) can use it.

Figure 6.12 The Select Files To Search dialog box (Solaris)


After making your selection and naming the file set, click the OK button. If you change your mind and don't want to save the file set, click Cancel.


Removing a File Set

To remove a previously-saved file set, choose Remove a file set from the File Sets pop-up menu in the Find dialog box. The CodeWarrior IDE displays the dialog box shown in Figure 6.14.

Select the file set you want to remove, then click the Remove button. The CodeWarrior IDE removes the file set so that future searches do not use the deleted file set. When you are finished removing file sets, click the Done button to return to the Find dialog box. If you change your mind about removing the file set, click Cancel instead.

Figure 6.13 The Save File Set dialog box



Controlling Search Range in Multiple Files

The CodeWarrior editor lets you search any number of files for a string. The files can be in the current project or any text file on disk. If you frequently search a particular set of files, just save that set and restore it later.

You can choose to stop searching at the end of each file or to search all files without stopping.

To treat all the files in the file set as one large file, turn off the Stop at End of File checkbox. When the editor reaches the end of one file, it starts searching the next file until the selected text is found. The editor beeps when it reaches the end of the last file to search. After text is found, you may resume searching for the next occurrence using the Find, Find Next, or Find Previous menu commands.

To search each file individually, enable the Stop at End of File checkbox. The editor beeps when it reaches the end of a file. The arrow to the left of the file set indicates the file the editor is currently searching.

Figure 6.14 Remove File Sets dialog box



You must choose Find in Next File from the Search Menu or use the Find in Previous File command to continue the search. To start the search from a particular file, just select the file and click in the column to its left.


Windows

The Find in Previous File command is available by pressing the key binding Ctrl-Shift-T.


Mac OS

The Find in Previous File command is available by pressing the Shift key while using the Search Menu.

After choosing your option, proceed just as you would if you were searching only one file.


TIP

You can search in the previously-searched file in the file list by using the Find in Previous File command. This effectively allows you to go backwards in your search into previously-searched files.

To learn more about text searching, see "Searching for Selected Text" on page 176, or "Finding and Replacing Text in Multiple Files" on page 185.


Using Regular Expressions (grep)

A regular expression is a text substring that is used as a mask for comparing text in a file. Regular expressions can be formed from single characters or from more complicated strings. When the regular expression is compared with the text in your file, the CodeWarrior IDE analyzes whether the text matches the regular expression you have entered.

This section discusses special operators in regular expressions that the CodeWarrior IDE recognizes and how they can be used to find and replace text. CodeWarrior's regular expressions are similar to UNIX's grep commands.


NOTE

Make sure the Regexp checkbox is selected in the Find dialog box.

This section consists of the following topics:


Special Operators

The following characters have special meanings based upon their placement in the regular expression. For more information, see "Using Regular Expressions" on page 197.

MetaCharacters
Description
"."
The match-any-character operator matches any single printing or non-printing character except newline and null.
"*"
The match-zero-or-more operator repeats the smallest preceding regular expression as many times as necessary (including zero) to match the pattern.
"+"
The match-one-or-more operator repeats the preceding regular expression at least once and then as many times as necessary to match the pattern.
"?"
The match-zero-or-one operator repeats the preceding regular expression once or not at all.
"\n"
The back-reference operator is used in the replace string to refer to a specified group in the find string. Each group must be enclosed within parentheses. The digit n must range between 1 and 9. The number identifies a specific group, starting from the left side of the regular expression.
"|"
The alternation operator matches one of a choice of regular expressions. If you place the alternation operator between any two regular expressions, the result matches the largest union of strings that it can match.
"^"
The match-beginning-of-line operator matches the string from the beginning of the string or after a newline character. When it appears within brackets the "^" represents a "not" action.
"$"
The match-end-of-line operator matches the string either at the end of the string or before a newline character in the string.
[]
List operators enable you to define a set of items to use as a match. The list items must be enclosed within square brackets. Note that you cannot define an empty list.
()
Group operators define sub-expressions that can be used elsewhere in the regular expression as a single unit.
"-"
The range operator defines the characters that fall between the start and ending characters within the list.


Using Regular Expressions

You can create powerful regular expressions to search for text and perform replace operations on found text. This section will discuss the following topics:

To give you a better idea of how regular expressions can improve search and replace functions, look over the following example code. Each concept discussed will refer to the example code shown in Listing 6.1.


Listing 6.1 Example Code


#include <iostream>

#define  var1  10;
#define  var2  20;

using namespace std;

int result = 0;

int main(void)
{
	cout << "This example provides information about" << endl;
	cout << "the use of regular expressions in the" << endl;
	cout << "CodeWarrior IDE." << endl << endl;
	cout << "Refer to this code when reading the" << endl;
	cout << "sections in the manual dealing with" << endl;
	cout << "the use of regular expressions." << endl << endl;
	cout << "The value of var1 is " << var1;
	cout << " out of 50, and" << endl;
	cout << "the value of var2 is " << var2;
	cout << " out of 50." << endl;
	cout << "$" << var1;
	cout << " + $" << var2;
	cout << " = $" <<;
	result += var1;
	result += var2;
	cout << result;
	cout << endl << endl;
	return 0;
}


Matching simple expressions

Most characters match themselves. For example, "a" matches all occurrences of the letter "a" in the code. The only exceptions are called special characters: the asterisk (*), plus sign (+), backslash (\), period (.), caret (^), square brackets ([ and ]), dollar sign ($), and ampersand (&). To match a special character, precede it with a backslash, like this: \*

For example, refer to the example code shown in Listing 6.1 on page 197. If you want to find every occurrence of the letter "m" in the code, you would type m into the Find text box. The CodeWarrior IDE will match this regular expression with the "m" in iostream. Additional searches will match with the "m" in namespace, main(void), example, information, and manual.

If you want to find every occurrence of a dollar sign in the code, you would type \$ in the Find text box. The backslash tells the IDE to interpret the dollar sign as a normal character rather than a special character. The IDE will match the regular expression with the first dollar sign in the following line from Listing 6.1:


cout << "$" << var1;


Additional searches will match with the second and third dollar signs in the next two lines.


Matching any character

A period (.) matches any character except a newline character. Use the period when you want to be more flexible in your search.

For example, refer to the example code shown in Listing 6.1 on page 197. If you want to find four-character expressions in the code that begin with var, type var. in the Find text box. The period following the var tells the IDE to look through the code for var immediately followed by a single character. The IDE will match this regular expression with var1 and var2 in the code.


Repeating expressions

The asterisk and plus sign are two special operators that allow you to "repeat" expressions in your search string:

For example, refer to the example code shown in Listing 6.1 on page 197. Suppose you type s*ion in the Find text box, The s* tells the IDE to match zero or more occurrences of the letter s just before the occurrence of ion. In Listing 6.1, this regular expression will match with the ion in information and sections from the following lines:


cout << "This example provides information about" << endl;
cout << "sections in the manual dealing with" << endl;

The same regular expression will also match with the ssion in expressions from the following lines:


cout << "the use of regular expressions in the" << endl;
cout << "the use of regular expressions." << endl << endl;

If you want to find matches that have at least one letter s preceding ion, you would type s+ion in the Find text box. The plus sign tells the IDE to match at least one occurrence of the letter s just before the occurrence of ion. This regular expression will match with the ssion in expressions from Listing 6.1. Notice that s+ion does not match with the ion in information or sections, since there is not at least one letter s preceding ion in either case.

If you want to find expressions in the code that contain the number zero, followed by one period or no periods at all, you would type 0\.? in the Find text box. The backslash tells the IDE to treat the period as a normal character, and the ? special operator acts on the normal period character. This expression will match with each occurrence of the number zero in Listing 6.1. This regular expression will also match with the 0. from the following line:


cout << " out of 50." << endl;


As shown in these examples, the asterisk and plus sign usually refer to a single character. However, it is possible to refer to more than one character at a time by grouping expressions. See the following section, Grouping expressions, for more information.


Grouping expressions

If an expression is enclosed in parentheses (( and )), the editor treats it as a single unit and applies any asterisk (*) or plus (+) to the whole expression.

For example, refer to the example code shown in Listing 6.1 on page 197. If you want to find expressions in the code that match is, you could simply type is in the Find text box. However, you could also use ( i)s as the regular expression. Notice that ( i)s tells the IDE to look for the letter s preceded by both a space and the letter i. Whereas is matches the is within This, this, and is, ( i)s will match only with is in the following two lines from Listing 6.1:


cout << "The value of var1 is " << var1;
cout << "The value of var2 is " << var2;


Choosing one character from many

A string of characters enclosed in square brackets ([]) matches any one character in that string. To match any character that is not in the string enclosed within the square brackets, precede the enclosed expression with a caret (^) like this: [^abc]

For example, refer to the example code shown in Listing 6.1 on page 197. If you want to find expressions in the code that contain the letters x, y, or z, you would type [xyz] in the Find text box. The IDE will match this regular expression with the letter x in example and expressions. If instead you want to find expressions in the code that do not contain the letters x, y, or z, type [^xyz] in the Find text box. The IDE will match this regular expression with every character in the example code except the letter x in example and expressions.

Placing a minus sign (-) within square brackets indicates a range of consecutive ASCII characters. For example, [0-9] is the same as [0123456789]. If the minus sign is the first or last character within the enclosed string, that minus sign loses its special meaning and is treated as an ordinary character. For example, [-bc] represents the minus sign and the letters b and c.

If a right square bracket is immediately after a left square bracket, it does not terminate the string but is considered to be one of the characters to match. For example, []0-9] tells the IDE to search for the right square bracket as well as any digit in the code. If any special character, such as backslash (\), asterisk (*), or plus sign (+), is immediately after the left square bracket, that special character is treated as an ordinary character. For example, [.] tells the IDE to search for periods in the code.

You can use square brackets in a similar manner as parentheses. The IDE will treat the information in the square brackets as a single unit. For example, [bsl]ag tells the IDE to search for occurrences of bag, sag, or lag in the code. Typing [aeiou][0-9] in the Find text box tells the IDE to search for a vowel followed by a number, such as a1.


Matching the beginning or end of a line

You can specify that a regular expression match only the beginning or end of the line.

For example, refer to the example code shown in Listing 6.1 on page 197. If you want to find expressions in the code that match cout and occur only at the beginning of a line, you would type ^([ \t]*cout) in the Find text box. The [ \t]* in the regular expression allows zero or more spaces and tabs to precede cout, as is the case with the code shown in Listing 6.1.


Using the Find string in the Replace string

You can include the contents of the Find string in the Replace string by using an ampersand (&) in the Replace string. For example, refer to the example code shown in Listing 6.1 on page 197. Suppose the Find string is var[0-9] and the Replace string is my_&. The editor will match the Find string with var1 and var2 in the code. Clicking the Replace button in the Find dialog box will replace var1 with my_var1 or var2 with my_var2.

To use an ampersand in the Replace without any special meaning, use \&. An ampersand has no special meaning in the Find string.


Remembering sub-expressions

You can remember sub-expressions of a regular expression in a Find string and recall those sub-expressions in the Replace string. You can create up to nine remembered sub-expressions for each Find string. Each sub-expression must be enclosed within parentheses. To recall these sub-expressions when typing the replace string, use \n, where n is a digit that specifies which sub-expression to recall. Determine n by counting sub-expressions from the left side of the Find string.

For example, refer to the example code shown in Listing 6.1 on page 197. If you want to change #define declarations into const declarations, you could perform a search for #define and manually change the line of code to a const declaration. However, you can take advantage of remembered sub-expressions to perform the same process automatically. Begin by typing the following regular expression in the Find text box:


\#define[ \t]+(.+)[ \t]+([0-9]+);


This regular expression tells the IDE to search for the following string, in the exact order given: #define, one or more spaces or tabs, one or more characters, one or more spaces or tabs, one or more digits, and a semicolon. Starting from the left side of the regular expression, the first sub-expression is (.+) and the second sub-expression is ([0-9]+). These two sub-expressions are recalled in the following Replace string:


const int \1 = \2;


The \1 refers to the first sub-expression and the \2 refers to the second sub-expression from the Find string. These two sub-expressions recall the variable name and its value from the original #define declaration. Notice that the replace string changes the #define declaration into a const declaration by using references to the two sub-expressions. Thus, the editor will find #define var1 10; and change it into const var1 = 10; in the code shown in Listing 6.1. It will change the next #define statement in the same manner.


References

For a comprehensive book on using regular expressions, get Mastering Regular Expressions, by Jeffrey E.F. Friedl, published by O'Reilly & Associates, Inc., from your local bookstore.





Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: support@metrowerks.com
Copyright © 1999, Metrowerks Corp. All rights reserved.

Last updated: May 24, 1999 * Chris Magnuson * John Roseborough